#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>

int main() {
    pid_t id = fork();
    if (id < 0) {
        printf("创建子进程失败！\n");
        exit(1);
    } else if (id == 0) {
        printf("我是子进程: pid: %d，我将异常退出！\n", getpid());
        int* p = NULL;
        *p = 1; // 引发异常
    } else {
        printf("我是父进程: pid: %d，我将耐心地等待子进程！\n", getpid());
        int status = 0;
        pid_t ret = waitpid(id, &status, 0);
        if (ret > 0) {
            // 等待成功
            if (WIFEXITED(status)) {
                printf("子进程正常退出，退出码: %d\n", WEXITSTATUS(status));
            } else if (WIFSIGNALED(status)) {
                printf("子进程异常退出，信号编号: %d\n", WTERMSIG(status));
            }
        }
    }
    return 0;
}
